﻿Imports System
Imports System.Collections.Generic

Namespace System.Workflow.Activities
    Friend Class CorrelationResolver
        ' Methods
        Friend Shared Function GetCorrelationProvider(ByVal interfaceType As Type) As ICorrelationProvider
            Return CorrelationResolver.GetResolver(interfaceType).CorrelationProvider
        End Function

        Private Shared Function GetResolver(ByVal interfaceType As Type) As CorrelationMethodResolver
            Dim resolver As CorrelationMethodResolver = Nothing
            CorrelationResolver.cachedTypeResolver.TryGetValue(interfaceType, resolver)
            If (resolver Is Nothing) Then
                SyncLock CorrelationResolver.mutex
                    CorrelationResolver.cachedTypeResolver.TryGetValue(interfaceType, resolver)
                    If (resolver Is Nothing) Then
                        resolver = New CorrelationMethodResolver(interfaceType)
                        CorrelationResolver.cachedTypeResolver.Add(interfaceType, resolver)
                    End If
                End SyncLock
            End If
            Return resolver
        End Function

        Friend Shared Function IsInitializingMember(ByVal interfaceType As Type, ByVal memberName As String, ByVal methodArgs As Object()) As Boolean
            If (interfaceType Is Nothing) Then
                Throw New ArgumentNullException("interfaceType")
            End If
            If (memberName Is Nothing) Then
                Throw New ArgumentNullException("memberName")
            End If
            If (memberName.Length = 0) Then
                Throw New ArgumentException(SR.GetString("Error_EventNameMissing"))
            End If
            Return CorrelationResolver.GetCorrelationProvider(interfaceType).IsInitializingMember(interfaceType, memberName, methodArgs)
        End Function

        Friend Shared Function ResolveCorrelationValues(ByVal interfaceType As Type, ByVal eventName As String, ByVal eventArgs As Object(), ByVal provideInitializerTokens As Boolean) As ICollection([Of] CorrelationProperty)
            If (interfaceType Is Nothing) Then
                Throw New ArgumentNullException("interfaceType")
            End If
            If (eventName Is Nothing) Then
                Throw New ArgumentNullException("eventName")
            End If
            If (eventName.Length = 0) Then
                Throw New ArgumentException(SR.GetString("Error_EventNameMissing"))
            End If
            Return CorrelationResolver.GetCorrelationProvider(interfaceType).ResolveCorrelationPropertyValues(interfaceType, eventName, eventArgs, provideInitializerTokens)
        End Function


        ' Fields
        Private Shared cachedTypeResolver As Dictionary([Of] Type, CorrelationMethodResolver) = New Dictionary([Of] Type, CorrelationMethodResolver)
        Private Shared mutex As Object = New Object
    End Class
End Namespace

